﻿using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.WriteAPI.Applications.Commands.DangerousCommands;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.WriteAPI.Controllers
{
    /// <summary>
    /// 事故管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DangerousController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<DangerousController> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public DangerousController(IMediator mediator, ILogger<DangerousController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 事故添加
        /// </summary>
        /// <param name="dangerous">事故实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>>AddDangerous(Dangerous dangerous)
        {
            try
            {
                AddDangerousCommand cmd = new AddDangerousCommand()
                { data = dangerous };
                logger.LogInformation("事故添加成功");
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError("事故添加出错" + ex.Message);
                throw;
            }
        }
    }
}
